import { h } from "preact";
import { useEffect, useState } from "preact/hooks";
import { subscribeToasts, removeToast } from "../utils/toastStore.ts";

interface ToastData {
  id: number;
  message: string;
  type: "success" | "error" | "info" | "warning";
  duration?: number;
}

// Toast样式函数
function getToastStyles(type: string) {
  const baseStyle = "fixed z-50 px-4 py-2 rounded-lg border shadow-md transition-all duration-300 flex items-center";
  
  switch (type) {
    case "success":
      return `${baseStyle} bg-green-50 text-green-700 border-green-200`;
    case "error":
      return `${baseStyle} bg-red-50 text-red-700 border-red-200`;
    case "warning":
      return `${baseStyle} bg-yellow-50 text-yellow-700 border-yellow-200`;
    case "info":
    default:
      return `${baseStyle} bg-blue-50 text-blue-600 border-blue-200`;
  }
}

// 单个Toast组件
function ToastItem({ id, message, type, duration = 3000 }: ToastData) {
  const [visible, setVisible] = useState(true);
  
  useEffect(() => {
    const timer = setTimeout(() => {
      setVisible(false);
    }, duration);
    
    return () => clearTimeout(timer);
  }, [duration]);
  
  // 渲染适当的图标
  const renderIcon = () => {
    switch(type) {
      case "success":
        return (
          <svg class="w-4 h-4 mr-2" viewBox="0 0 20 20" fill="currentColor">
            <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clipRule="evenodd" />
          </svg>
        );
      case "error":
        return (
          <svg class="w-4 h-4 mr-2" viewBox="0 0 20 20" fill="currentColor">
            <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
          </svg>
        );
      case "warning":
        return (
          <svg class="w-4 h-4 mr-2" viewBox="0 0 20 20" fill="currentColor">
            <path fillRule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clipRule="evenodd" />
          </svg>
        );
      case "info":
      default:
        return (
          <svg class="w-4 h-4 mr-2" viewBox="0 0 20 20" fill="currentColor">
            <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-11a1 1 0 10-2 0v4a1 1 0 102 0V7zm-1-5a1 1 0 100 2 1 1 0 000-2z" clipRule="evenodd" />
          </svg>
        );
    }
  };

  return (
    <div
      class={`${getToastStyles(type)} ${visible ? "opacity-100" : "opacity-0 translate-y-[-10px]"}`}
      style={{
        top: "20px",
        left: "50%",
        transform: "translateX(-50%)"
      }}
      role="alert"
    >
      {renderIcon()}
      <span>{message}</span>
    </div>
  );
}

// Toast容器组件
export default function ToastContainer() {
  const [toasts, setToasts] = useState<ToastData[]>([]);
  
  useEffect(() => {
    // 订阅通知更新
    return subscribeToasts((newToasts) => {
      setToasts(newToasts);
    });
  }, []);
  
  if (toasts.length === 0) return null;
  
  return (
    <div class="fixed inset-0 pointer-events-none flex flex-col items-center z-50 p-4 overflow-hidden">
      {toasts.map((toast) => (
        <div key={toast.id} class="mb-2 pointer-events-auto">
          <ToastItem
            id={toast.id}
            message={toast.message}
            type={toast.type}
            duration={toast.duration}
          />
        </div>
      ))}
    </div>
  );
} 